﻿using System;
using System.Collections.Generic;
using System.Data;

namespace BestPrice.Common
{
    public abstract class BaseKey
    {
        [NonSerialized]
        private String _EntityName;
        private String EntityName
        {
            get
            {
                if (String.IsNullOrEmpty(_EntityName))
                {
                    String name = this.GetType().Name;
                    _EntityName = name.Substring(0, name.Length - 3);
                }

                return _EntityName;
            }
        }

        [NonSerialized]
        private String _QualifiedEntitySetName;
        private String QualifiedEntitySetName
        {
            get
            {
                if (String.IsNullOrEmpty(_QualifiedEntitySetName))
                    _QualifiedEntitySetName = CoreSetting.EntityContainer + EntityName;
                return _QualifiedEntitySetName;
            }
        }

        [NonSerialized]
        private Boolean _IsFixed = false;
        public Boolean IsFixed
        {
            get { return _IsFixed; }
            set { _IsFixed = value; }
        }

        [NonSerialized]
        private EntityKey _Key = null;
        public EntityKey Key
        {
            get
            {
                if (_Key == null || !_IsFixed)
                    _Key = new EntityKey(QualifiedEntitySetName, Members);
                return _Key;
            }
        }

        [NonSerialized]
        protected KeyValuePair<String, Object>[] _Members;
        public KeyValuePair<String, Object>[] Members
        {
            get
            {
                var e = EntityName;
                if (_Members == null || !_IsFixed)
                    _Members = Util.KeyValuePairs(this);
                return _Members;
            }
        }

        public BaseKey(Boolean isFixed = false)
        {
            _IsFixed = isFixed;
        }
    }
}
